package com.shujia.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Code23Action4 {
  def main(args: Array[String]): Unit = {
    val sc = new SparkContext(new SparkConf().setMaster("local").setAppName("CoGroup"))

    val value1RDD: RDD[(String, Int)] = sc.parallelize(List(("k1", 11), ("k1", 1), ("k2", 2), ("k3", 3), ("k5", 5), ("k4", 4), ("k3", 3), ("k3", 33)), 2)

//    val collectRes: collection.Map[String, Int] = value1RDD
//      .collectAsMap()
//
//    println(collectRes)

    /**
     * reduceByKeyLocally:
     *    Action算子，相当于 reduceByKey + collectAsMap 算子
     *
     */
    val reduceByKeyRes1: collection.Map[String, Int] = value1RDD
      .reduceByKey(_ + _)
      .collectAsMap()
    println(reduceByKeyRes1)

    val reduceByKeyRes2: collection.Map[String, Int] = value1RDD
      .reduceByKeyLocally(_ + _)

    println(reduceByKeyRes2)


  }
}
